/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.gitee.minimalismstyle.fresh.oauth.service.impl

import com.gitee.minimalismstyle.fresh.common.data.beetl.service.impl.LongModelBaseServiceImpl
import com.gitee.minimalismstyle.fresh.oauth.api.model.OauthClientDetails
import com.gitee.minimalismstyle.fresh.oauth.api.service.OauthClientDetailsService
import com.gitee.minimalismstyle.fresh.oauth.dao.OauthClientDetailsDao
import org.springframework.context.annotation.Primary
import org.springframework.security.oauth2.provider.ClientDetails
import org.springframework.security.oauth2.provider.client.BaseClientDetails
import org.springframework.stereotype.Service
import org.springframework.util.StringUtils

/**
 * 业务实现
 * @author maochao
 */
@Primary
@Service
class OauthClientDetailsServiceImpl :
        OauthClientDetailsService, LongModelBaseServiceImpl<OauthClientDetailsDao, OauthClientDetails>() {

    override fun loadClientByClientId(clientId: String): ClientDetails {
        val oauthClientDetails = dao.findByClientId(clientId)
        val clientDetails = BaseClientDetails(
                oauthClientDetails.clientId,
                oauthClientDetails.resourceIds,
                oauthClientDetails.scope,
                oauthClientDetails.authorizedGrantTypes,
                oauthClientDetails.authorities,
                oauthClientDetails.webServerRedirectUri
        )
        clientDetails.clientSecret = oauthClientDetails.clientSecret
        clientDetails.setAutoApproveScopes(StringUtils.commaDelimitedListToSet(oauthClientDetails.autoapprove))
        return clientDetails
    }
}